#include "bdd.h"

row *
get_row (row * head, char *se, byte c_s)
{
  row *tmp;
  char *we = NULL;
  uint h;
  if (c_s == NO_CASE_SENS)
    {
      we = dup_low_string (se);
      h = chksum_crc32 (we, strlen (we));
    }
  else
    h = chksum_crc32 (se, strlen (se));

  for (tmp = head; tmp; tmp = tmp->next)
    {
      if (h == tmp->hash)
	{
	  if (c_s == NO_CASE_SENS)
	    {
	      if (!strcmp (tmp->ident, we))
		{
		  free (we);
		  return tmp;
		}
	    }
	  else if (!strcmp (tmp->ident, se))
	    return tmp;
	}
    }
  if (we)
    free (we);
  return NULL;
}

session *
get_session (unsigned int si)
{
  if (si > sess_top || !sess_tab[si] || !si)
    return NULL;

  return sess_tab[si];
}

subbase *
get_subbase (subbase * head, char *se, byte c_s)
{
  subbase *tmp;
  char *we = NULL;
  uint h;
  if (c_s == NO_CASE_SENS)
    {
      we = dup_low_string (se);
      h = chksum_crc32 (we, strlen (we));
    }
  else
    h = chksum_crc32 (se, strlen (se));

  for (tmp = head; tmp; tmp = tmp->next)
    {
      if (h == tmp->hash)
	{
	  if (c_s == NO_CASE_SENS)
	    {
	      if (!strcmp (tmp->ident, we))
		{
		  free (we);
		  return tmp;
		}
	    }
	  else if (!strcmp (tmp->ident, se))
	    return tmp;
	}
    }
  if (we)
    free (we);
  return NULL;
}

racine *
get_base (racine * head, char *se)
{
  racine *tmp;
  uint h = chksum_crc32 (se, strlen (se));

  for (tmp = head; tmp; tmp = tmp->next)
    if (h == tmp->base->hash && !strcmp (tmp->base->ident, se))
      return tmp;
  return NULL;
}

racine *
get_racine_by_base (racine * head, subbase * ptr)
{
  racine *run;
  for (run = head; run; run = run->next)
    if (run->base == ptr)
      return (run);
  return NULL;
}
